package org.huangrui.javase.io;

import org.junit.Test;

import java.io.*;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Objects;

/**
 * @Author hr
 * @Create 2025-11-08 0:47
 **/
public class T04CompareFile {
    @Test
    public void test1()throws Exception{
        File f1 = new File("d:\\1.txt");
        File f2 = new File("d:\\2.txt");
        File f3 = new File("/1.txt");
        System.out.println(smallFile(f1, f2)?"相同":"不相同");
        System.out.println(smallFile(f1, f3)?"相同":"不相同");
    }
    public boolean smallFile(File f1, File f2) throws IOException {
        if(Files.isSameFile(f1.toPath(),f2.toPath())){//比较是不是同一个文件
            return true;
        }
        if(f1.length() != f2.length()){//比较文件大小
            return false;
        }
        //整个文件一次读取完比较
        byte[] b1 = Files.readAllBytes(f1.toPath());
        byte[] b2 = Files.readAllBytes(f2.toPath());
        return Arrays.equals(b1,b2);
    }

    @Test
    public void test2()throws Exception{
        File f1 = new File("d:\\1.txt");
        File f2 = new File("d:\\2.txt");
        System.out.println(bigFile(f1, f2)?"相同":"不相同");
    }

    public boolean bigFile(File f1, File f2) throws IOException {
        if(Files.isSameFile(f1.toPath(),f2.toPath())){//比较是不是同一个文件
            return true;
        }
        if(f1.length() != f2.length()){//比较文件大小
            return false;
        }

        try(FileInputStream fis1 = new FileInputStream(f1);
            FileInputStream fis2 = new FileInputStream(f2);
        ) {
            //最多一次比较1024个字节
            byte[] b1 = new byte[2];
            byte[] b2 = new byte[2];
            while (true) {
                int length1 = fis1.read(b1);
                int length2 = fis2.read(b2);

                if (length1 == -1 && length2 == -1) {
                    return true;
                }
                if (!Arrays.equals(b1, b2)) {
                    return false;
                }
            }
        }catch (IOException e){
            throw new IOException(e);
        }
    }

    @Test
    public void test3()throws Exception{
        File f1 = new File("d:\\1.txt");
        File f2 = new File("d:\\2.txt");
        System.out.println(TxtFile(f1, f2)?"相同":"不相同");
    }
    public boolean TxtFile(File f1, File f2) throws IOException {
        if(Files.isSameFile(f1.toPath(),f2.toPath())){//比较是不是同一个文件
            return true;
        }
        if(f1.length() != f2.length()){//比较文件大小
            return false;
        }
        try(
                FileReader fr1 = new FileReader(f1);
                BufferedReader b1 = new BufferedReader(fr1);

                FileReader fr2 = new FileReader(f2);
                BufferedReader b2 = new BufferedReader(fr2);

        ){
            //逐行读取并比较
            while(true){
                String s1 = b1.readLine();
                String s2 = b2.readLine();

                if(s1 == null && s2 == null){
                    return true;
                }
                if(!Objects.equals(s1,s2)){
                    return false;
                }
            }
        }catch (IOException e){
            throw new IOException(e);
        }
    }

}
